package ca.uwaterloo.mylongcall;

import java.util.Set;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;

public class SP extends Activity{
	SharedPreferences.Editor editor;
	SharedPreferences settings;
	
	final String Name_Set = "Name_Set";
	final String Pin_Set = "Pin_set";
	final String Total_CallCard_Number = "Total_CallCard_Number";
	final String Total_Contact_Number ="Total_Contact_Number";
	final String _PRE = "_PRE";
	final String _PHONE = "_PHONE";
	
	public SP() {
		settings = getSharedPreferences("MylongCall",MODE_PRIVATE);
	}
	
	public void remove(String key) {
		editor = settings.edit();
		editor.remove(key);
		editor.commit();
	}
	
	public void putin(String key, String value) {
		editor = settings.edit();
		editor.putString(key,value);
		editor.commit();
	}
	
	public void putin(String key, int value) {
		editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	@SuppressLint("NewApi")
	public void putin(String key, Set<String> value) {
		editor = settings.edit();
		editor.putStringSet(key, value);
		editor.commit();
	}
	
	public int getCallCardnumber() {
		return settings.getInt("Total_CallCard_Number",0);
	}
	
	public int getContactnumber() {
		return settings.getInt("Total_Contact_Number", 0);
	}
	
	public CallCard getDefaultCallCard() {
		String defaultPin = settings.getString("Default_Pin", "");
		String pre = settings.getString(defaultPin+"_PRE", "");
		
		CallCard a = null;
		if (defaultPin.length()>0 && pre.length()>0) {
			a = new CallCard(pre, defaultPin);
		}
		return a;
	}
	
	@SuppressLint("NewApi")
	public CallCard[] getAllCallCards() {
		Set<String> PinSet = settings.getStringSet("Pin_Set", null);
		String[] PinArray = (String[]) PinSet.toArray();
		int l = PinArray.length;
		String[] preArray = new String[l];
		for (int i=0; i<l ; i++) {
			preArray[i] = settings.getString(PinArray[i]+"_PRE", "");
		}
		
		CallCard[] CardArray = new CallCard[l];
		for (int i=0; i<l ; i++) {
			CardArray[i] = new CallCard(preArray[i], PinArray[i]);
		}
		
		return CardArray;	
	}
	
	@SuppressLint("NewApi")
	public Contact[] getAllContacts() {
		Set<String> NameSet = settings.getStringSet("Name_Set", null);
		
		if (NameSet != null) {
			String[] NameArray = (String[]) NameSet.toArray();
			int l = NameArray.length;
		
			String[] PhoneArray = new String[l];
			Contact[] ContactArray = new Contact[l];
		
			for (int i=0; i<l; i++) {
				PhoneArray[i] = settings.getString(NameArray[i]+"_PHONE", "");
				ContactArray[i] = new Contact(NameArray[i], PhoneArray[i]);
			}
		
			return ContactArray;
		}
		else return null;
	}

	@SuppressLint("NewApi")
	public void ChangeDefaultCallCard(CallCard a) {
		String Pin = a.getPin();
		remove("Default_Pin");
		putin("Default_Pin", Pin);		
	}
	
	@SuppressLint("NewApi")
	public void AddCallCard(CallCard a) {
		String Pin = a.getPin();
		String Pre = a.getPre();
		
		Set<String> PinSet = settings.getStringSet("Pin_Set", null);
		PinSet.add(Pin);
		
		remove("Pin_Set");
		putin("Pin_Set", PinSet);
		putin(Pin+"PRE", Pre);	
		
		int current = settings.getInt("Total_CallCard_Number", 0);
		current++;
		remove("Total_CallCard_Number");
		putin("Total_CallCard_Number",current);	
		
	}
	
	@SuppressLint("NewApi")
	public void AddContact(Contact a) {
		Set<String> NameSet = settings.getStringSet("Name_Set", null);
		NameSet.add(a.getName());
		remove("Name_Set");
		putin("Name_Set",NameSet);
		
		int current = settings.getInt("Total_Contact_Number", 0);
		current++;
		remove("Total_Contact_Number");
		putin("Total_Contact_Number",current);
	}
}
